package com.lecture.springlecture.controller;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.DemoDataListener;
import com.lecture.java8.excel.model.FinanceResult;
import com.lecture.springlecture.config.MyConfigBean;
import com.lecture.springlecture.dao.UserDao;
import com.lecture.springlecture.entity.User;
import com.lecture.springlecture.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;

/**
 * @program: spring-lecture
 * @description:
 * @author: luyn
 * @create: 2020-03-20 17:37
 **/
@RestController
@RequestMapping("api")
public class MyController {

    @Value("${myConfig.myUser.name}")
    private String name;

    @Value("${myConfig.myUser.age}")
    private int age;

    @Autowired
    private MyConfigBean myConfigBean;

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    @RequestMapping("/user")
    public Object getUser() {
        System.out.println("name：" + this.name);
        System.out.println("age：" + this.age);
        System.out.println(myConfigBean.getName() + " : " + myConfigBean.getAge());
        return User.builder().age(age).birthday(new Date()).id(1L).name(name).build();
    }

    @GetMapping("/getUserByName")
    public User getUserByName(String name) {
        System.out.println("输入参数：" + name);
        User user = userService.getUserByName(name);
        System.out.println(user);
        return user;
    }

    @RequestMapping("saveUser")
    public String saveUser() {
        User user = User.builder().age(age).birthday(new Date()).id(2L).name(name).build();
        userService.saveUser(user);
        return "success";
    }


    @RequestMapping("getXml")
    public String test(String xml) {
        System.out.println(xml);
        return xml;
    }

    @GetMapping("finance")
    public void finance() {
        try {
            /*String fileName = "D:\\workspace\\2023深能\\知识案例\\1\\data.xlsx";
            DemoDataListener<Finance> dataListener = new DemoDataListener<>();
            EasyExcel.read(fileName, Finance.class,dataListener).sheet().doRead();
            List<Finance> financeList = dataListener.getDataList();
            financeList = financeList.stream().filter(finance -> {
                return "2012".equals(finance.getYear());
            }).collect(Collectors.toList());
            userDao.saveFinance(financeList);*/

            String fileName = "D:\\workspace\\2023深能\\知识案例\\1\\result.xlsx";
            DemoDataListener<FinanceResult> dataListener2 = new DemoDataListener<>();
            EasyExcel.read(fileName, FinanceResult.class,dataListener2).sheet().doRead();
            List<FinanceResult> financeResultList = dataListener2.getDataList();
            userDao.saveFinanceResult(financeResultList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("getImage")
    public void getImage(HttpServletResponse response) {
        ServletOutputStream os = null;
        try {
            byte[] bytes = getPic("");
            os = response.getOutputStream();
            os.write(bytes);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public byte[] getPic(String picName) {
        //进行图片路径处理。拼接上图片名
        final Path path = Paths.get("D:\\workspace\\1708418023383.jpg");
        byte[] bytes = null;
        if (path.toFile().exists()) {
            try {
                bytes = Files.readAllBytes(path);
            } catch (Exception e) {
            }
        }else {}
        return bytes;
    }
}
